package com.mac.intellect_flow.admin.controller.common;


import com.mac.intellect_flow.enums.ResultCodeEnum;
import com.mac.intellect_flow.framework.util.FileUploadUtil;
import com.mac.intellect_flow.reply.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@Tag(name = "文件模块")
@RestController
@RequestMapping("/admin/file")
public class FileController {

    @Autowired
    private FileUploadUtil fileUploadUtil;

    @Operation(summary = "上传单个文件")
    @Parameter(name = "file", description = "文件对象")
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file){
        try {
            return R.success(fileUploadUtil.upload(file));
        } catch (Exception e) {
            return R.error(ResultCodeEnum.FILE_UPLOAD_ERROR);
        }
    }

    @Operation(summary = "下载文件")
    @Parameter(name = "path", description = "文件路径")
    @PostMapping("/download")
    public R download(String path,HttpServletResponse response){
        try {
            fileUploadUtil.download(path,response);
            return R.success();
        } catch (Exception e) {
            return R.error(ResultCodeEnum.FILE_DOWNLOAD_ERROR);
        }
    }
}
